<?php
include_once '../include/config.php';
include_once '../class/mysql.class.php';
/*---
	用户列表更新
*/
$userid = $_SESSION[$_chat['session_userid']];
$username = $_SESSION[$_chat['session_username']];
$query = selectData('*','online',"`user_id` = '$userid' and `username` = '$username'");
if(mysql_num_rows($query) == 1){
	$where = "`user_id` = '$userid' AND `username` = '$username'";
	$value = 'time='.date('YmdHis');
	updateData('online',$value,$where);
}
if(mysql_num_rows($query) == 0){
	$insertValue = array('time'=>date('YmdHis'),'user_id'=>$userid,'username'=>$username);
	insertData('online',$insertValue);
}
$deleteTime = date('Y-m-d H:i:s',strtotime('- '.$_chat['referer'].' Second'));
deleteData('online',"`time` < '$deleteTime'");
/*---
	 杩斿洖XML鏂囦欢
*/
$xml = '<myName>'.$username.'</myName>';
switch ($_GET['type']){
	case 'list': //鐢ㄦ埛鍒楄〃
		$query = selectData('`time`,`user_id`,`username`','online');
		while($row = mysql_fetch_array($query)){
			if($row['username'] != $username){
				$xml .= '<user><id>'.$row['user_id'].'</id><name>'.$row['username'].'</name><time>'.$row['time'].'</time></user>';
			}
		}
		break;
	case 'amount': //鐢ㄦ埛鏁伴噺
		$query = selectData('`id`','online',"`username` != '$username'");
		$xml = '<amount>'.mysql_num_rows($query).'</amount>';
		break;
	case 'receive': //鎺ュ彈淇℃伅
		$to = isset($_GET['to']) ? $_GET['to'] : null;
		$query = selectData('`id`,`time`,`msg`','msg',"`to` = '$username' AND `form` = '$to' AND mark = '1'");
		while($row = mysql_fetch_array($query)){
			$id = $row['id'];
			if($row['msg'] != null){
				$xml .= '<receive><user>'.$to.'</user><time>'.date('H:i:s',strtotime($row['time'])).'</time><msg>'.$row['msg'].'</msg></receive>';
			}
			updateData('msg',"mark=0","`id` = '$id'");
		}
		break;
	case 'send': //鍙戦�淇℃伅
		$msg = isset($_GET['msg']) ? $_GET['msg'] : null;
		$to = isset($_GET['to']) ? $_GET['to'] : null;
		if($msg != null && $to != null){
			$value = array('time'=>date('YmdHis'),'form'=>$username,'to'=>$to,'msg'=>$msg,'mark'=>'1');
			insertData('msg',$value);
		}else{
			$msg = '鍙戦�澶辫触';
		}
		$xml = '<send><user>'.$username.' '.date('H:i:s').'</user><msg>'.$msg.'</msg></send>';
		break;
}
header('Content-Type:text/html');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<CATALOG>'.$xml.'</CATALOG>';
?>